/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package idea.icci.explorer;


import idea.de.explorer.IDataElemsTopComponent;
import idea.model.dbmanager.QueryManager;
import idea.model.qnrlibrary.CustomContentItems;
import idea.model.qnrlibrary.CustomResponseDeView;
import java.awt.event.ActionEvent;
import java.beans.IntrospectionException;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JOptionPane;
import org.openide.nodes.BeanNode;
import org.openide.nodes.ChildFactory;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.util.lookup.Lookups;
import org.openide.windows.WindowManager;


/**
 *
 * @author rejun001
 */
public class ICustomContentItemChildFactory extends ChildFactory<CustomContentItems>
{
    private List<CustomContentItems> resultList;

    /**
     * xtor
     * @param resultList 
     */
    public ICustomContentItemChildFactory( List<CustomContentItems> resultList )
    {
        this.resultList = resultList;
    }

    /**
     * Create the list of node keys to display
     * @param list
     * @return 
     */
    @Override
    protected boolean createKeys( List<CustomContentItems> list )
    {
        for (CustomContentItems cci : resultList) {
            list.add(cci);
        }
        return true;
    }

    /**
     * Create a node out of the current key
     * @param key (the bean or whatever is the underlying data object)
     * @return the created node
     */
    @Override
    protected Node createNodeForKey( CustomContentItems key )
    {
        ICustomContentNode node = null;
        try
        {
            node = new ICustomContentNode(key);
        } catch ( Exception e )
        {
            Exceptions.printStackTrace( e );
        }
        return node;
    } 
    
    /**
     * Inner class: create a CustomContentNode out of a CustomContentItem
     */
    private class ICustomContentNode extends BeanNode {
        private String currItemId = null;
        private String currItemWording = null;
        private String currSeq = null;
        private String currItemNumber = null;

        /**
         * xtor
         * @param bean
         * @throws IntrospectionException 
         */
        public ICustomContentNode( CustomContentItems bean ) throws IntrospectionException
        {
            super( bean , Children.LEAF, Lookups.singleton( bean ) );
            currItemId = bean.getItemId();
            currItemWording = bean.getItemWording();
            currSeq = bean.getItemSequence().toString();
            currItemNumber = bean.getItemNumber();
            
            //setDisplayName(bean.getItemNumber());
            setDisplayName(currSeq);
            setShortDescription(currItemNumber + "<br\\>" + currItemWording);
            //setShortDescription(bean.getItemWording());
        }

        /**
         * Get the array of Actions for context menu
         * @param bln
         * @return 
         */
        @Override
        public Action[] getActions( boolean bln )
        {
            Action[] oldActions = super.getActions( bln ); // this would be the Properties and possibly the WHERE clause stuff
            Action[] newActions = new Action[oldActions.length + 3];
            System.arraycopy( oldActions , 0 , newActions , 0 , oldActions.length );
            //newActions[oldActions.length] = new ShowDesignerAction();
            newActions[oldActions.length] = new DrillDownAction();
            newActions[oldActions.length + 1] = new DeleteAction();
            return newActions;
        }

        /**
         * Override the double click (preferred) action
         * side effect: set up drillDown as the new preferred action.
         * 
         * @return null so the default (get Properties) won't happen
         * 
         */
        @Override
        public Action getPreferredAction()
        {
            Action a;
            a = super.getPreferredAction();
            System.out.println( "Preferred Action! " + a.toString() + ", wording: " + currItemWording + ", item_id is " + currItemId );
            drillDown();
            
            return null;
        }
        
        /**
         * The actual drillDown functionality
         */
        private void drillDown() {
            List<CustomResponseDeView> dataElems = QueryManager.getDataElements( currItemId );
            
            IDataElemsTopComponent detc = (IDataElemsTopComponent) WindowManager.getDefault().findTopComponent( "IDataElemsTopComponent");
            detc.displayDataElements(dataElems, currSeq, currItemNumber);
            detc.open();
            detc.requestActive();
        }
        
        /**
         * Inner class for the DrillDown Action
         */
        private class DrillDownAction extends AbstractAction
        {
            /**
             * xtor.  putValue creates the context menu item wording
             */
            public DrillDownAction()
            {
                putValue( NAME , "Drill Down" );
            }

            /**
             * Sets up DrillDown as the Action's action.
             * @param e 
             */
            @Override
            public void actionPerformed( ActionEvent e )
            {
                drillDown();
            }
        }
        
        /**
         * Not working yet
         */
        private class DeleteAction extends AbstractAction //implements Presenter.Popup
        {
            public DeleteAction()
            {
                putValue( NAME , "Delete..." );
            }

            @Override
            public void actionPerformed( ActionEvent e )
            {
                CustomContentItems obj = getLookup().lookup( CustomContentItems.class );
                JOptionPane.showMessageDialog( null , "Delete: " + obj.getItemWording() );
            }
        }
    }
}

